#include "flight.h"

Node_list *SeqListCreat() // 单向链表创建
{
    Node_list *Head = (Node_list *)malloc(sizeof(Node_list));
    if (Head == NULL)
    {
        printf("%s creat error!\n", __func__);
        return NULL;
    }

    memset(&Head->info, 0, sizeof(Data_type));
    Head->next = NULL;

    return Head;
}

int SeqListHeadInsert(Node_list *Head, Data_type info) // 插入航班信息(尾插)
{
    Node_list *temp;
    temp = (Node_list *)malloc(sizeof(Node_list));
    if (temp == NULL)
    {
        printf("%s headinsert error!\n", __func__);
        return -1;
    }
    while (Head->next != NULL)
    {
        Head = Head->next;
    }
    temp->info = info;
    temp->next = Head->next;
    Head->next = temp;

    return 0;
}

int SeqListShow(Node_list *Head) // 遍历航班信息
{
    // 1、判空
    if (Head == NULL)
    {
        printf("%s head is empty!\n", __func__);
        return -1;
    }
    while (Head->next != NULL)
    {
        Head = Head->next;
        printf("航班号:%s", Head->info.number);
        printf("起点站:%s", Head->info.number);
        printf("终点站:%s", Head->info.arraddress);
        printf("班期:%s", Head->info.DATE);
        printf("机型:%s", Head->info.TYPE);
        printf("起飞时间:%s", Head->info.stime);
        printf("到达时间:%s", Head->info.atime);
        printf("票价:%d", Head->info.value);

        putchar(10);
    }
}

void SeqListWriteFile(Node_list *Head) // 写入航班信息到文件
{
    FILE *fp = fopen("./filght.txt", "w");
    if (fp == NULL)
    {
        printf("打开文件失败!\n");
        return;
    }
    while (Head->next != NULL)
    {
        Head = Head->next;
        fprintf(fp, "%s:%s:%s:%s:%s:%s:%s:%d\n",
                Head->info.number, Head->info.number, Head->info.arraddress,
                Head->info.DATE, Head->info.TYPE, Head->info.stime, Head->info.atime, Head->info.value);
    }
    fclose(fp);
}

void SeqListReadFile(Node_list *Head) // 读取航班信息到链表
{
    char getinfo[200];
    Data_type temp;
    char *s;
    FILE *fp = fopen("./filght.txt", "r");
    if (fp == NULL)
    {
        printf("文件打开失败!\n");
        return;
    }
    while ((fgets(getinfo, sizeof(getinfo), fp)) != NULL)
    {
        s = strtok(getinfo, ":");
        strcpy(temp.number, s);

        s = strtok(NULL, ":");
        strcpy(temp.staddress, s);

        s = strtok(NULL, ":");
        strcpy(temp.arraddress, s);

        s = strtok(NULL, ":");
        strcpy(temp.staddress, s);

        s = strtok(NULL, ":");
        strcpy(temp.DATE, s);

        s = strtok(NULL, ":");
        strcpy(temp.TYPE, s);

        s = strtok(NULL, ":");
        strcpy(temp.DATE, s);

        s = strtok(NULL, ":");
        strcpy(temp.stime, s);

        s = strtok(NULL, ":");
        strcpy(temp.atime, s);

        s = strtok(NULL, ":");
        temp.value = atoi(s);
    }
    fclose(fp);
}

Data_type putcc()
{
    Data_type info;
    printf("航班号\n");
    scanf("%s", info.number);

    printf("起点站\n");
    scanf("%s", info.number);

    printf("终点站\n");
    scanf("%s", info.number);

    printf("班期\n");
    scanf("%s", info.number);

    printf("机型\n");
    scanf("%s", info.number);

    printf("起飞时间\n");
    scanf("%s", info.number);

    printf("到达时间\n");
    scanf("%s", info.number);

    printf("票价\n");
    scanf("%d", &info.value);

    return info;
}